package com.panda.delivery.react;

import android.content.Intent;
import android.text.TextUtils;

import com.panda.delivery.BuildConfig;
import com.panda.delivery.activity.LookPicActivity;
import com.panda.delivery.activity.MainActivity;
import com.panda.delivery.dialog.ConfirmDialog;
import com.panda.delivery.dialog.OkDialog;
import com.panda.delivery.model.DialogBean;
import com.panda.delivery.model.Picture;
import com.panda.delivery.service.RecordNotificationService;
import com.panda.delivery.utils.JsonConvert;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiyukf.unicorn.api.ConsultSource;
import com.qiyukf.unicorn.api.Unicorn;
import com.qiyukf.unicorn.api.YSFUserInfo;


/**
 * Created by Administrator on 2018/4/15.
 */

public class MyModule extends ReactContextBaseJavaModule {
    ReactApplicationContext mContext;

    public MyModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
    }

    @Override
    public String getName() {
        return "MyModule";
    }

    @ReactMethod
    public void handleMessage(ReadableMap data, Promise promise) {
        try {
            if (data.hasKey("type")) {
                String type = data.getString("type");
                Intent intent=null;
                switch (type) {
                    case "getCodeKey":
                        promise.resolve(BuildConfig.CODEPUSH_KEY);
                        break;
                    case "lookBigPic":
                        String productImage = JsonConvert.reactToJSON(data.getMap("pictures")).toString();
                        Picture commentImage = new ObjectMapper().readValue(productImage, Picture.class);
                        intent = new Intent(mContext, LookPicActivity.class);
                        intent.putExtra("pictures", commentImage.getPath());
                        intent.putExtra("position", commentImage.getIndex());
                        if (getCurrentActivity() != null) {
                            getCurrentActivity().startActivity(intent);
                        }
                        break;
                    case "okDialog":
                        OkDialog okDialog = new OkDialog(getCurrentActivity(), new ObjectMapper().readValue(JsonConvert.reactToJSON(data.getMap("data")).toString(), DialogBean.class), promise);
                        okDialog.showPopupWindow();
                        break;
                    case "startRecord":
//                        intent = new Intent(getCurrentActivity(), RecordNotificationService.class);
//                        getCurrentActivity().startService(intent);
                        break;
                    case "stopRecord":
//                        if(intent!=null)
//                            getCurrentActivity().startService(intent);
                        break;
                    case "confirmDialog":
                        ConfirmDialog confirmDialog = new ConfirmDialog(getCurrentActivity(), new ObjectMapper().readValue(JsonConvert.reactToJSON(data.getMap("data")).toString(), DialogBean.class), promise);
                        confirmDialog.showPopupWindow();
                        break;
                    case "ysfLogin":
                        YSFUserInfo userInfo = new YSFUserInfo();
                        // App 的用户 ID
                        userInfo.userId = data.getString("userId");
                        // 当且仅当开发者在管理后台开启了 authToken 校验功能时，该字段才有效
//                        userInfo.authToken = "auth-token-from-user-server";
                        // CRM 扩展字段
                        userInfo.data = data.getString("data");
//                        Log.d("更新会话用户资料", data.getString("data"));
                        Unicorn.setUserInfo(userInfo);
                        break;
                    case "ysfLogout":
                        Unicorn.logout();
                        break;
                    case "openConversation":
                        String page = data.getString("page");
                        String title = data.getString("title");
                        String groupId = data.getString("groupId");
                        String staffId = data.getString("staffId");
                        /**
                         * 设置访客来源，标识访客是从哪个页面发起咨询的，用于客服了解用户是从什么页面进入。
                         * 三个参数分别为：来源页面的url，来源页面标题，来源页面额外信息（保留字段，暂时无用）。
                         * 设置来源后，在客服会话界面的"用户资料"栏的页面项，可以看到这里设置的值。
                         */
                        ConsultSource source = new ConsultSource(page, title, "");
                        if (!TextUtils.isEmpty(groupId)) {
                            source.groupId = Long.parseLong(groupId);//指定分组
                        }
                        if (!TextUtils.isEmpty(staffId)) {
                            source.staffId = Long.parseLong(staffId);//指定客服
                        }

                        /**
                         * 请注意： 调用该接口前，应先检查Unicorn.isServiceAvailable()，
                         * 如果返回为false，该接口不会有任何动作
                         *
                         * @param context 上下文
                         * @param title   聊天窗口的标题
                         * @param source  咨询的发起来源，包括发起咨询的url，title，描述信息等
                         */
                        Unicorn.openServiceActivity(mContext, title, source);
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
